package com.tdkj.action.backstage.friendlylink;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.apache.struts2.ServletActionContext;
import org.hibernate.Criteria;
import org.hibernate.criterion.Order;

import com.tdkj.action.BaseAction;
import com.tdkj.model.friendlylink.BeanFriendlylink;
import com.tdkj.model.information.BeanInfoComment;
import com.tdkj.service.friendlylink.IBeanFriendlylinkService;
import com.tdkj.util.FileUtil;


public class FriendlylinkAction extends BaseAction {
	private IBeanFriendlylinkService friendlylinkService;
	private long id;
	private String title;
	private String linkurl;
	private String imgurl;
	private List<BeanFriendlylink> links=new ArrayList<BeanFriendlylink>();
	private String[] ids;
	public String deleteAll() {
		if (null != ids) {
			for (String idtemp : ids) {
				this.getBaseService().delete(Long.parseLong(idtemp), BeanFriendlylink.class);
			}
		}
		return findAll();
	}
	public long getId() {
		return id;
	}
	public void setId(long id) {
		this.id = id;
	}
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	public String getImgurl() {
		return imgurl;
	}
	public void setImgurl(String imgurl) {
		this.imgurl = imgurl;
	}
	public String getLinkurl() {
		return linkurl;
	}
	public void setLinkurl(String linkurl) {
		this.linkurl = linkurl;
	}
	public List<BeanFriendlylink> getLinks() {
		return links;
	}
	public void setLinks(List<BeanFriendlylink> links) {
		this.links = links;
	}
	public IBeanFriendlylinkService getFriendlylinkService() {
		return friendlylinkService;
	}
	@Resource
	public void setFriendlylinkService(IBeanFriendlylinkService friendlylinkService) {
		this.friendlylinkService = friendlylinkService;
	}
	
	//进入编辑界面
	public String getEditPage(){
		BeanFriendlylink bean=friendlylinkService.findById(this.getId(), BeanFriendlylink.class);
		this.setId(bean.getId());
		this.setTitle(bean.getTitle());
		this.setLinkurl(bean.getLinkurl());
		this.setImgurl(bean.getImgurl());
		return "editPage";
	}
	
	//进入添加界面
	public String getAddPage(){
		return "addPage";
	}
	
	//获取列表
	public String findAll(){
		links=friendlylinkService.findAll(BeanFriendlylink.class);
		if(links.size()==0)appendObj(20);
		Criteria criteria = this.createCriteria(BeanFriendlylink.class);
		criteria.addOrder(Order.desc("createtime"));// 对状态进行排序
		this.queryByPage(criteria, "friendlylinkAction!findAll.action", 10);
		return "findAll";
	}
	
	public String editFriendlylink(){
		BeanFriendlylink bean=friendlylinkService.findById(this.getId(), BeanFriendlylink.class);
		bean.setImgurl(this.getImgurl());
		bean.setLinkurl(this.getLinkurl());
		bean.setTitle(this.getTitle());
		friendlylinkService.merge(bean);
		return findAll();
	}
	
	public String addFriendlylink(){
		BeanFriendlylink bean=new BeanFriendlylink();
		bean.setTitle(this.getTitle());
		bean.setLinkurl(this.getLinkurl());
		bean.setImgurl(this.getImgurl());
		bean.setCreatetime(new Timestamp(System.currentTimeMillis()));
		friendlylinkService.save(bean);
		return findAll();
	}
	//添加链接           
	public String deleteFriendlylink() throws Exception{
		BeanFriendlylink bean=friendlylinkService.findById(this.getId(), BeanFriendlylink.class);
		String filePath=bean.getImgurl();
		String path =ServletActionContext.getServletContext().getRealPath("/");
		path= path.replace('\\', '/')+"/"+filePath;
		FileUtil.deleteFile(path);
		friendlylinkService.delete(this.getId(), BeanFriendlylink.class);
		return findAll();
	}

	private  void appendObj(int num) {
		// TODO Auto-generated method stub
		BeanFriendlylink bean=null;
		for(int i=0;i<num;i++){
			bean=new BeanFriendlylink();
			bean.setCreatetime(new Timestamp(System.currentTimeMillis()));
			bean.setTitle("友情链接"+i);
			bean.setImgurl("images/"+i+".png");
			bean.setLinkurl("http://www.baidu.com");
			friendlylinkService.save(bean);
		}
	}
}
